﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TTServer.Controller
{
    public class MouseController
    {

        [DllImport("user32.dll")]
        static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        [Flags]
        public enum MouseEventFlags
        {
            LEFTDOWN = 0x00000002,
            LEFTUP = 0x00000004,
            MIDDLEDOWN = 0x00000020,
            MIDDLEUP = 0x00000040,
            MOVE = 0x00000001,
            ABSOLUTE = 0x00008000,
            RIGHTDOWN = 0x00000008,
            RIGHTUP = 0x00000010
        }

        public static readonly int BUTTON_LEFT = 1;
        public static readonly int BUTTON_MIDDLE = 2;
        public static readonly int BUTTON_RIGHT = 3;


        public static readonly int STATE_DOWN = 1;
        public static readonly int STATE_UP = 2;
        public static readonly int STATE_DOWNUP = 3;

        public static void LeftClick()
        {
            mouse_event((int)(MouseEventFlags.LEFTDOWN), 0, 0, 0, 0);
            mouse_event((int)(MouseEventFlags.LEFTUP), 0, 0, 0, 0);
        }

        public static void RightClick()
        {
            mouse_event((int)(MouseEventFlags.RIGHTDOWN), 0, 0, 0, 0);
            mouse_event((int)(MouseEventFlags.RIGHTUP), 0, 0, 0, 0);
        }

        public static void MiddleClick()
        {
            mouse_event((int)(MouseEventFlags.MIDDLEDOWN), 0, 0, 0, 0);
            mouse_event((int)(MouseEventFlags.MIDDLEUP), 0, 0, 0, 0);
        }

        // If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain normalized 
        // absolute coordinates between 0 and 65,535
        public static void RelativeMove(int dx, int dy)
        {
            mouse_event((int)(MouseEventFlags.MOVE), dx, dy, 0, 0);
        }

        public static void processMouseEvent(int button, int state)
        { 
            int flag = 0;

            if (button == BUTTON_LEFT && state == STATE_DOWN)
            {
                flag = (int) MouseEventFlags.LEFTDOWN;
            }
            else if (button == BUTTON_LEFT && state == STATE_UP)
            {
                flag = (int)MouseEventFlags.LEFTUP;
            }
            else if (button == BUTTON_LEFT && state == STATE_DOWNUP)
            {
                LeftClick();
            }
            else if (button == BUTTON_RIGHT && state == STATE_DOWN)
            {
                flag = (int)MouseEventFlags.RIGHTDOWN;
            }
            else if (button == BUTTON_RIGHT && state == STATE_UP)
            {
                flag = (int)MouseEventFlags.RIGHTUP;
            }
            else if (button == BUTTON_RIGHT && state == STATE_DOWNUP)
            {
                RightClick();
            }
            else if (button == BUTTON_MIDDLE && state == STATE_DOWNUP)
            {
                MiddleClick();
            }


            if (flag != 0)
            {
                mouse_event(flag, 0, 0, 0, 0);
            }
        }
    }
}
